package cn.tedu.thread;

public class DeadLockDemo {
    //创建对象
    private static Printer p=new Printer();
    private static Scaner s=new Scaner();
    public static void main(String[] args) {
        //创建线程对象---描述第一个员工干活
        new Thread(new Runnable() {
            @Override
            public void run() {
                //先打印再扫描
                synchronized (p){
                    //打印
                    p.print();
                    //休眠
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    //扫描
                    synchronized (s){
                        //扫描
                        s.scan();
                    }
                }
            }
        }).start();

        //创建线程对象---描述第二个员工干活
        new Thread(new Runnable() {
            @Override
            public void run() {
                //先扫描再打印
                synchronized (s){
                    //扫描
                    s.scan();
                    //休眠
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (p){
                        //打印
                        p.print();
                    }
                }
            }
        }).start();
    }
}


//代表打印机类
class Printer{
    public void print(){
        System.out.println("在哼哧哼哧的打印...");
    }
}

//代表扫描仪的类
class Scaner{
    public void scan(){
        System.out.println("在呼哧呼哧的扫描...");
    }
}


















